const { sendData, successData, sendCode } = require("@/utils/r/result");
const gin_context = require("@/utils/gin_context");
const menuService = require("@/server/menu");

const menu = {
  /**
   * 获取当前用户的菜单,获取当前用户菜单: 生成后台管理界面的菜单
   * @param {*} req
   * @param {*} res
   */
  async GetUserMenu(req, res) {
    //先查询当前保存在session中的用户id，根据id查询菜单列表
    let result = await menuService.GetUserMenuTree(
      gin_context.GetFromContext(req, "user_info_id")
    );

    successData(res, result);
  },
  async GetTreeList(req, res) {
    let query = req.query || {};
    successData(res, await menuService.GetTreeList(query));
  },
  async SaveOrUpdate(req, res) {
    sendCode(
      res,
      await menuService.SaveOrUpdate(req.body, req["user_info_id"])
    );
  },
  async Delete(req, res) {
    const id = req.params.id;
    sendCode(res, await menuService.Delete(id));
  },
  async GetOption(req, res) {
    successData(res, await menuService.GetOptionList());
  },
};

module.exports = menu;
